// pages/orderTime/index.js
var dateTimePicker = require('../../../utils/dateTimePicker.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    showModalStatus: false,
    dateTimeArray1: null,
    dateTime1: null,
    dateTimeArray2: null,
    dateTime2: null,
    calendar:[],
    width:0,
    currentIndex:-1,
    currentTime:'',
    InputBottom: 0,
    parkinfo:[],
    SpaceNumberList:[],//SpaceNumberList为某个停车场的的所有车位编码
    ordermsg:[{
      parkname:null,
      // orderstarttime:null,
      // time:null,
      // orderprice:null,
      name:null,
      portnum:null,
    }
    ]
  },
   changeDateTime2(e) {
    this.setData({ dateTime2: e.detail.value });
   },
  changeDateTimeColumn2(e) {
     var arr = this.data.dateTime2, dateArr = this.data.dateTimeArray2;
     arr[e.detail.column] = e.detail.value;
     
     dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
  this.setData({ 
       dateTimeArray2: dateArr,
        dateTime2: arr
     });
},
  async getnewsDetail(parkid){
    var that=this;
    wx.request({
      url: 'http://localhost:8080/blog/park/getidlist?parkid='+parkid,
      success: (result) => {
        that.setData({
          parkinfo: result.data.data.list
        })     
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    const {parkid}=options;
      this.getnewsDetail(parkid);
       // 获取完整的年月日 时分秒，以及默认显示的数组
       var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
       this.setData({
         dateTimeArray1: obj1.dateTimeArray,
         dateTime1: obj1.dateTime
       });
      // 获取完整的年月日 时分秒，以及默认显示的数组
      var obj2 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
      this.setData({
      dateTimeArray2: obj2.dateTimeArray,
      dateTime2: obj2.dateTime
    });
    var that=this;
    function getThisMonthDays(year, month) {
      return new Date(year, month, 0).getDate();
    }
  // 计算每月第一天是星期几
    function getFirstDayOfWeek(year, month) {
      return new Date(Date.UTC(year, month - 1, 1)).getDay();
    }
    const date = new Date();
    const cur_year = date.getFullYear();
    const cur_month = date.getMonth() + 1;
    const cur_date=date.getDate();
    const weeks_ch = ['日', '一', '二', '三', '四', '五', '六'];
    //利用构造函数创建对象
    function calendar(date,week){
      this.date=cur_year+'-'+cur_month+'-'+date;
      if(date==cur_date){
        this.week = "今天";
      }else if(date==cur_date+1){
        this.week = '星期' + week;
      }else{
        this.week = '星期' + week;
      }
    }
    //当前月份的天数
    var monthLength= getThisMonthDays(cur_year, cur_month)
    //当前月份的第一天是星期几
    var week = getFirstDayOfWeek(cur_year, cur_month)
    var x = week;
    for(var i=1;i<=monthLength;i++){
      //当循环完一周后，初始化再次循环
      if(x>6){
        x=0;
      }
      //利用构造函数创建对象
      that.data.calendar[i] = new calendar(i, [weeks_ch[x]][0])
      x++;
    }
    //限制要渲染的日历数据天数为7天以内（用户体验）
    var flag = that.data.calendar.splice(cur_date, that.data.calendar.length - cur_date <= 7 ? that.data.calendar.length:7)
    that.setData({
      calendar: flag
    })
    //设置scroll-view的子容器的宽度
    that.setData({
      width: 186 * parseInt(that.data.calendar.length - cur_date <= 7 ? that.data.calendar.length : 7)
    })
  },
  formsubmit:function(option){
    const ousername = option.detail.value.name;
    const oportnum = this.data.currentTime;
    // const odata = new Date(option.detail.target.dataset.starttime);
    // const otime = option.detail.target.dataset.time;
    // const oprice = option.detail.target.dataset.orderprice;
    const oparkname = this.data.parkinfo[0].parkname;
    const ostatus = 0;
    if(ousername!='' && oportnum!=''){
       wx.request({
        url: 'http://localhost:8080/ipark/order/saveorder',
        data:{oparkname,
              // odata,
              // otime,
              // oprice,
              ousername,
              oportnum,
              ostatus},
        header: { 
          'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
      },
        method: 'POST',
        success: (result) => {
          wx.showModal({
            title: '您已预约成功，请在15分钟内到达停车场，逾期预约将自动取消',  
            showCancel:false,
          })
        },
      })
    }else{
      wx.showModal({
        title: '请完善预约信息',
        showCancel:false,
      })
    }

  },
  onShareAppMessage: function () {
  },
  select:function(event){
    //为上半部分的点击事件
    this.setData({
      currentIndex: event.currentTarget.dataset.index,
    })
    //  console.log(event.currentTarget.dataset)
  },
  //车位选择部分的点击事件
  selectTime:function(event){
    this.setData({
      currentIndex: event.currentTarget.dataset.tindex,
      currentTime: event.currentTarget.dataset.time
    })
    // console.log(event.currentTarget.dataset)
  },

  //根据停车场id显示车位信息的接口方法
  powerDrawer: function (e) { 
    const oparkname = this.data.parkinfo[0].parkname
    const parkid =  this.data.parkinfo[0].parkid;
    // console.log(oparkname)
    // console.log(parkid)
    var that=this;
    wx.request({
      url: 'http://localhost:8080/ipark/parkspace/getSpaceNumberList?parkid='+parkid,
      success: (result) => {
        that.setData({
          // parkinfo: result.data.data.list
          SpaceNumberList : result.data.data.list
        })    
        // console.log(result.data.data.list) 
      }
    });
    var currentStatu = e.currentTarget.dataset.statu;
    this.util(currentStatu)
  },

  util: function(currentStatu){
    /* 动画部分 */
    // 第1步：创建动画实例 
    var animation = wx.createAnimation({
      duration: 200,  //动画时长
      timingFunction: "linear", //线性
      delay: 0  //0则不延迟
    });
    
    // 第2步：这个动画实例赋给当前的动画实例
    this.animation = animation;
 
    // 第3步：执行第一组动画：Y轴偏移240px后(盒子高度是240px)，停
    animation.translateY(240).step();
 
    // 第4步：导出动画对象赋给数据对象储存
    this.setData({
      animationData: animation.export()
    })
    
    // 第5步：设置定时器到指定时候后，执行第二组动画
    setTimeout(function () {
      // 执行第二组动画：Y轴不偏移，停
      animation.translateY(0).step()
      // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象
      this.setData({
        animationData: animation
      })
      
      //关闭抽屉
      if (currentStatu == "close") {
        this.setData(
          {
            showModalStatus: false
          }
        );
      }
    }.bind(this), 200)
  
    // 显示抽屉
    if (currentStatu == "open") {
      this.setData(
        {
          showModalStatus: true
        }
      );
    }
  },
  
  changeDateTime1(e) {
        this.setData({ dateTime1: e.detail.value });
       },
  changeDateTimeColumn1(e) {
         var arr = this.data.dateTime1, dateArr = this.data.dateTimeArray1;    
         arr[e.detail.column] = e.detail.value;
         dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);    
      this.setData({ 
           dateTimeArray1: dateArr,
          dateTime1: arr
         });
   },
})